<template>
  <view class="container">
    <view class="title">车间5S检测</view>
    <view class="login-form">
      <view class="form-item">
        <text class="label">用户名：</text>
        <input class="input" v-model="username" placeholder="请输入用户名"/>
      </view>
      <view class="form-item">
        <text class="label">密码：</text>
        <input class="input" type="password" v-model="password" placeholder="请输入密码"/>
      </view>
      <button class="login-btn" @click="login">登录</button>
    </view>
  </view>
</template>

<script>
import request from '@/utils/request';

export default {
  data() {
    return {
      username: '', // 用户名
      password: '' // 密码
    };
  },
  methods: {
    login() {
      // 登录逻辑
      if (this.username.trim() === '' || this.password.trim() === '') {
        uni.showToast({
          title: '用户名和密码不能为空',
          icon: 'none'
        });
        return;
      }

      // 使用封装好的 request 发送 POST 请求
      request.post('/user/login', {
        loginName: this.username,
        password: this.password
      })
      .then(response => {
        // 登录成功
        uni.showToast({
          title: '登录成功',
          icon: 'success'
        });
        // 跳转到提交页面
        uni.redirectTo({
          url: '/pages/submit/submit'
        });
      })
      .catch(error => {
        // 登录失败
        uni.showToast({
          title: error || '登录失败',
          icon: 'none'
        });
      });
    }
  }
}
</script>

<style>
.container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #B2DAE1 0%, #1C9DBF 100%);
  padding: 20px;
}

.title {
  font-size: 28px;
  font-weight: bold;
  color: #fff;
  margin-bottom: 40px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.login-form {
  width: 100%;
  max-width: 400px;
  background-color: #fff;
  padding: 30px;
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease-in-out;
}

.login-form:hover {
  transform: translateY(-10px);
}

.form-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.label {
  width: 80px;
  font-size: 20px;
  color: #333;
}

.input {
  flex: 1;
  border: 1px solid #ddd;
  padding: 12px;
  border-radius: 8px;
  font-size: 16px;
  transition: border-color 0.3s ease-in-out;
  margin-top: -8px;
}

.input:focus {
  border-color: #007AFF;
  box-shadow: 0 0 0 2px rgba(0, 122, 255, 0.3);
}

.login-btn {
  width: 100%;
  height: 50px;
  line-height: 50px;
  background-color: #7AB4CC;
  color: #fff;
  border-radius: 8px;
  font-size: 18px;
  border: none;
  cursor: pointer;
  transition: background-color 0.3s ease-in-out;
}

.login-btn:hover {
  background-color: #0066cc;
}
</style>